﻿///
/// Authors: Lee Gary 
///
/// Date: 24/09/2010
///
/// Purpose: 
///
/// Using LinQ to XML to parse and extract required information to instantiate GSP objects
/// and returning a List<T> 
/// 
/// The core of transforming gibberish SOAP data from SharePoint into more human readable
/// and manageable data
/// 
/// Future:
/// 1.) Modification to extension method instead 
/// 2.) Enhance overall performance 
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using GSPMigratorCore.GSPObjects;

namespace GSPMigratorCore.Linq2XML
{
    public class GSPLinQtoXMLHelper
    {
        public List<GSPFolder> ParseFoldersXML(XmlNode xNode)
        {
            List<GSPFolder> GSPFolders = new List<GSPFolder>();
            XElement xdoc = XElement.Parse(xNode.InnerXml, LoadOptions.None);
            var query =
              from e in xdoc.Elements()
              select new
              {
                  CreatedBy = e.Attribute("ows_Author").Value.ToString().Substring(e.Attribute("ows_Author").Value.ToString().IndexOf("#") + 1), //CreatedBy
                  CreatedDate = DateTime.Parse(e.Attribute("ows_Modified").Value), //CreatedDate
                  URL = e.Attribute("ows_EncodedAbsUrl").Value, //AbsURL
                  SiteSlashParentFolder = e.Attribute("ows_FileDirRef").Value.ToString().Substring(e.Attribute("ows_FileDirRef").Value.ToString().IndexOf("#") + 1), //Parent
                  Name = e.Attribute("ows_LinkFilename").Value.ToString().Substring(e.Attribute("ows_LinkFilename").Value.ToString().IndexOf("/") + 1)
              };
            foreach (var obj in query)
            {
                GSPFolder folder = new GSPFolder();

                folder.CreatedBy = obj.CreatedBy;
                folder.CreatedDate = obj.CreatedDate;
                folder.Name = obj.Name;
                folder.SiteSlashParentFolder = obj.SiteSlashParentFolder;
                folder.URL = obj.URL;

                GSPFolders.Add(folder);
            }

            return GSPFolders;
        }

        public List<GSPDocLib> ParseDocLibXML(XmlNode xNode, bool hideDefaultDocLib)
        {

            List<GSPDocLib> DocLibs = new List<GSPDocLib>();
            XElement xdoc = XElement.Parse(xNode.OuterXml.ToString());
            var query =
              from e in xdoc.Elements()
              where e.Attribute("BaseType").Value == "1" //SPBaseType.DocumentLibrary = 1
              select new
              {
                  Title = e.Attribute("Title").Value,
                  WebFullUrl = e.Attribute("WebFullUrl").Value
              };

            foreach (var obj in query)
            {
                if (!hideDefaultDocLib)
                {
                    GSPDocLib tempDocLib = new GSPDocLib();
                    tempDocLib.Title = obj.Title;
                    tempDocLib.WebFullUrl = obj.WebFullUrl.Substring(1);
                    DocLibs.Add(tempDocLib);
                }
                else
                {
                    string[] defaultDocLibs = { "List Template Gallery", "Master Page Gallery",
                                               "Site Template Gallery", "Web Part Gallery" };
                    if (!defaultDocLibs.Contains(obj.Title))
                    {
                        GSPDocLib tempDocLib = new GSPDocLib();
                        tempDocLib.Title = obj.Title;
                        tempDocLib.WebFullUrl = obj.WebFullUrl.Substring(1);
                        DocLibs.Add(tempDocLib);
                    }
                }
            }

            return DocLibs;
        }

        public List<GSPListItem> ParseSPListItemsXML(XmlNode xNode, out List<GSPFolder> GSPFolders)
        {
            GSPFolders = new List<GSPFolder>();
            List<GSPListItem> GSPListItems = new List<GSPListItem>();
            XElement xdoc = XElement.Parse(xNode.InnerXml, LoadOptions.None);
            var query =
              from e in xdoc.Elements()
              select new
              {
                  FileName = e.Attribute("ows_LinkFilenameNoMenu").Value,
                  WebFullUrl = e.Attribute("ows_EncodedAbsUrl").Value,
                  ContentType = e.Attribute("ows_ContentType").Value,
                  CreatedBy = e.Attribute("ows_Author").Value.ToString().Substring(e.Attribute("ows_Author").Value.ToString().IndexOf("#") + 1), //CreatedBy
                  CreatedDate = DateTime.Parse(e.Attribute("ows_Modified").Value), //CreatedDate
                  URL = e.Attribute("ows_EncodedAbsUrl").Value, //AbsURL
                  SiteSlashParentFolder = e.Attribute("ows_FileDirRef").Value.ToString().Substring(e.Attribute("ows_FileDirRef").Value.ToString().IndexOf("#") + 1), //Parent
                  Name = e.Attribute("ows_LinkFilename").Value.ToString().Substring(e.Attribute("ows_LinkFilename").Value.ToString().IndexOf("/") + 1)
              
              };
            foreach (var obj in query)
            {
                if (obj.ContentType.Equals("Folder"))
                {
                    GSPFolder folder = new GSPFolder();

                    folder.CreatedBy = obj.CreatedBy;
                    folder.CreatedDate = obj.CreatedDate;
                    folder.Name = obj.Name;
                    folder.SiteSlashParentFolder = obj.SiteSlashParentFolder;
                    folder.URL = obj.URL;

                    GSPFolders.Add(folder);
                }
                else
                {
                    GSPListItem item = new GSPListItem();

                    item.FileName = obj.FileName;
                    item.WebFullUrl = obj.WebFullUrl;

                    GSPListItems.Add(item);
                }
            }

            return GSPListItems;
        }
    }
}
